(function (Cofe, Spine, $) {
    var Models = Cofe.Models;
    var File = Models.File;
    var Folder = Models.Folder;

    var EntryList = Spine.Model.setup("EntryList",  //Models name
    ["status", "entries", "path"]);  //Models attributes.

    EntryList.extend(Spine.Model.Local);

    EntryList.include({
        loadEntries: function (entryData) {
            var folders = new Array();
            var files = new Array();
            var folderIdx = 0;
            var fileIdx = 0;

            for (var i = 0, il = entryData.length; i < il; i++) {
                var currentEntry = entryData[i];
                var newEntry;
                if (currentEntry.basetype == "Folder") {
                    newEntry = Folder.init(currentEntry);
                    folders[folderIdx++] = newEntry;
                }
                else {
                    newEntry = File.init(currentEntry);
                    files[fileIdx++] = newEntry;
                }
            }

            this.folders = folders;
            this.files = files;
        },

        load: function (data) {
            this.status = data.status;
            this.entries = data.entries;
            this.loadEntries(data.entries);
        },
        init: function (data) {
            this.path = this.id = data.path;
            this.status = data.status;
            this.entries = data.entries;
            if (data.entries)
                this.loadEntries(data.entries);
//            console.log("EntryList created: Path=" + data.path);
        }

    });

    EntryList.include({
        url: "",
        fetch: function () {
            EntryList.trigger("fetching", this);
            var url = "/lookup/" + this.path + "?rel=list&json=true"
            var jqxhr = $.get(url,
                this.proxy(function (data) {
                    this.load(data);
                    EntryList.trigger("fetched", this);
                }))
//                .success(function () { alert("second success"); })
                .error(function (xhr, s, e) { EntryList.trigger(xhr, s, e); })
//                .complete(function () { alert("complete"); });
        }
    });    

    if (typeof exports !== "undefined") {
        Models.EntryList = exports;
    } else {
        Models.EntryList = EntryList;
    }

})(Cofe, Spine, Spine.$);